package com.qdairlines.service.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qdairlines.common.Page;
import com.qdairlines.dao.user.UserMapper;
import com.qdairlines.entity.user.User;
import com.qdairlines.service.base.impl.BaseServiceImpl;
import com.qdairlines.service.user.UserService;


@Service
public class UserServiceImpl  extends BaseServiceImpl<User,Integer> implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(userMapper);
	}

	@Override
	public User getUserByWorkcode(String workcode) {
		User queryUser  = new User();
		queryUser.setWorkcode(workcode);
		List<User> list = this.listData(queryUser);
		if(list != null && list.size()==1){
			//如果查询有且仅有一个用户，则返回该用户
			return list.get(0);
		}
		return null;
	}

	@Override
	public User getUserByUserName(String userName) {
		User queryUser  = new User();
		queryUser.setUserName(userName);
		List<User> list = this.listData(queryUser);
		if(list != null && list.size()==1){
			//如果查询有且仅有一个用户，则返回该用户
			return list.get(0);
		}
		return null;
	}
	

	@Override
	public List<User> listUserByRoleId(Integer roleId) {
		return userMapper.listUserByRoleId(roleId);
	}

	@Override
	public Page listUserByQuery(String queryKey,Page page) {
		PageHelper.startPage(page.getPageNumber(), page.getPageSize());
		List<User> list = userMapper.selectByQueryKey(queryKey);
		PageInfo<User> pageInfo = new PageInfo<User>(list);
		page.setTotal(String.valueOf(pageInfo.getTotal()));
		page.setRows(pageInfo.getList());
		return page;
	}


	@Override
	public List<User> listUserByRoleIdNotRelate(Integer roleId) {
		return userMapper.listUserByRoleIdNotRelate(roleId);
	}  


	
}
